package net.realsee.controller.wcs.device;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import net.realsee.common.annotation.Log;
import net.realsee.common.annotation.RepeatSubmit;
import net.realsee.common.enums.BusinessType;
import net.realsee.common.utils.poi.ExcelUtil;
import net.realsee.domain.wcs.device.bo.PlcTagTreeNode;
import net.realsee.domain.wcs.device.bo.WcsPlcTagBo;
import net.realsee.domain.wcs.device.vo.WcsPlcTagVo;
import net.realsee.framework.core.controller.BaseController;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.domain.R;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.validate.AddGroup;
import net.realsee.framework.core.validate.EditGroup;
import net.realsee.framework.plc.PlcOperateResult;
import net.realsee.service.wcs.device.IWcsPlcTagService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * IOT TAG
 *
 * @author realsee
 * @date 2023-08-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wcs/plcTag")
public class WcsPlcTagController extends BaseController {

    private final IWcsPlcTagService iWcsPlcTagService;

    /**
     * 查询IOT TAG列表
     */
    @SaCheckPermission("wcs:plcTag:list")
    @GetMapping("/list")
    public TableDataInfo<WcsPlcTagVo> list(WcsPlcTagBo bo, PageQuery pageQuery) {
        return iWcsPlcTagService.selectVoPage(bo, pageQuery);
    }

    /**
     * 导出IOT TAG列表
     */
    @SaCheckPermission("wcs:plcTag:export")
    @Log(title = "IOT TAG", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WcsPlcTagBo bo, HttpServletResponse response) {
        List<WcsPlcTagVo> list = iWcsPlcTagService.selectVoList(bo);
        ExcelUtil.exportExcel(list, "IOT TAG", WcsPlcTagVo.class, response);
    }

    /**
     * 获取IOT TAG详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("wcs:plcTag:query")
    @GetMapping("/{id}")
    public R<WcsPlcTagVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(iWcsPlcTagService.selectVoById(id));
    }

    /**
     * 新增IOT TAG
     */
    @SaCheckPermission("wcs:plcTag:add")
    @Log(title = "IOT TAG", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WcsPlcTagBo bo) {
        return toAjax(iWcsPlcTagService.insertByBo(bo));
    }

    /**
     * 修改IOT TAG
     */
    @SaCheckPermission("wcs:plcTag:edit")
    @Log(title = "IOT TAG", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WcsPlcTagBo bo) {
        return toAjax(iWcsPlcTagService.updateByBo(bo));
    }

    /**
     * 删除IOT TAG
     *
     * @param ids 主键串
     */
    @SaCheckPermission("wcs:plcTag:remove")
    @Log(title = "IOT TAG", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iWcsPlcTagService.deleteWithValidByIds(Arrays.asList(ids)));
    }

    /**
     * 查询TAG树
     */
    @SaCheckPermission("wcs:plcTag:list")
    @GetMapping("/tree")
    public R<PlcTagTreeNode> tree() {
        return R.ok(iWcsPlcTagService.tree());
    }

    /**
     * 获取点位当前值
     *
     * @param tagCode tagCode
     */
    @SaCheckPermission("wcs:plcTag:query")
    @GetMapping("/current/content")
    public R<PlcOperateResult> currentContent(@NotNull(message = "tagCode不能为空") @RequestParam String tagCode) {
        return R.ok(iWcsPlcTagService.currentContent(tagCode));
    }

    /**
     * 写入点位
     *
     * @param tagCode tagCode
     */
    @SaCheckPermission("wcs:plcTag:query")
    @GetMapping("/write")
    public R<PlcOperateResult> write(@NotNull(message = "tagCode不能为空") @RequestParam String tagCode,
                                     @NotNull(message = "值不能为空") @RequestParam String value) {
        return R.ok(iWcsPlcTagService.write(tagCode, value));
    }

}
